/**
 * 2017年10月30日
 */
package exp.core;

import java.io.FileWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;

import exp.algorithm.sic.ScalePeak;
import exp.algorithm.sic.TimeSeries;
import exp.api.InstancesConverter;
import exp.api.TemplateSaver;
import exp.entity.ChartDataset;
import freemarker.template.Configuration;
import freemarker.template.Template;
import weka.core.Instances;

/**
 * @author Alex
 *
 */
@Component
public class PythonPlotWriter  implements TemplateSaver {
	final String suffix = ".py";
	@Value("#{setting[template_save_dir]}")
	String dir ;
	@Autowired Configuration cfg;
	@Override
	public void save(Instances inst, String saveName, String templateName) throws Exception {
		throw new UnsupportedOperationException(saveName+","+templateName);
	}

	@Override
	public void save(String saveName, String tName,TimeSeries original, TimeSeries[] smoothed, ScalePeak[]peaks) throws Exception {
		Template t = cfg.getTemplate(tName);
		Map<String,Object> f = Maps.newHashMap();
		f.put("original", original);
		f.put("smoothed", smoothed);
		f.put("peaks",peaks);
		Writer w = new FileWriter(FileUtils.getFile(dir,saveName.endsWith(".py")?saveName:(saveName+suffix)));
		t.process(f, w);
		w.close();
	
	}

}
